<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="租户名称" prop="tenantName">
        <el-input
          v-model="queryParams.tenantName"
          placeholder="请输入租户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户类型" prop="tenantType">
        <el-select v-model="form.tenantType" placeholder="请选择租户类型">
          <el-option
            v-for="dict in dict.type.tenant_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tenantInfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租户名称" align="center" prop="tenantName" />
      <el-table-column label="租户头像" align="center" prop="tenantAvatar">
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.tenantAvatar"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="租户电话" align="center" prop="tenantTel" />
      <el-table-column label="租户管理者" align="center" prop="tenantLeader" />
      <el-table-column label="租户类型" align="center" prop="tenantType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.tenant_type"
            :value="scope.row.tenantType"
          />
        </template>
      </el-table-column>
      <el-table-column label="租户状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.tenant_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="租户地址" align="center" prop="tenantAddress" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="configDataSource(scope.row)"
            >数据库配置</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改后台租户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="租户名称" prop="tenantName">
          <el-input v-model="form.tenantName" placeholder="请输入租户名称" />
        </el-form-item>
        <el-form-item label="租户头像" prop="tenantAvatar">
          <image-upload v-model="form.tenantAvatar" :limit="1" />
        </el-form-item>
        <el-form-item label="租户电话" prop="tenantTel">
          <el-input v-model="form.tenantTel" placeholder="请输入租户电话" />
        </el-form-item>
        <el-form-item label="租户管理者" prop="tenantLeader">
          <el-input
            v-model="form.tenantLeader"
            placeholder="请输入租户管理者"
          />
        </el-form-item>
        <el-form-item label="签约年数" prop="tenantTime" v-if="form.id == null">
          <el-input
            type="number"
            v-model="form.tenantTime"
            min="1"
            placeholder="请输入签约年数"
          />
        </el-form-item>
        <el-form-item label="租户类型" prop="tenantType">
          <el-select v-model="form.tenantType" placeholder="请选择任务分组">
            <el-option
              v-for="dict in dict.type.tenant_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="租户AppID" prop="tenantAppid">
          <el-input v-model="form.tenantAppid" placeholder="请输入租户AppID" />
        </el-form-item>
        <el-form-item label="租户AppSecret" prop="tenantAppsecret">
          <el-input
            v-model="form.tenantAppsecret"
            placeholder="请输入租户AppSecret"
          />
        </el-form-item>
        <el-form-item label="租户地址" prop="tenantAddress">
          <el-input v-model="form.tenantAddress" placeholder="请输入租户地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 数据配置操作 -->
    <el-dialog
      :title="title"
      :visible.sync="sourceOpen"
      width="700px"
      append-to-body
    >
      <el-form
        ref="sourceForm"
        :model="sourceForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="数据库名称" prop="dbName">
          <el-input
            v-model="sourceForm.dbName"
            placeholder="请输入数据库名称"
            :disabled="constData"
          />
        </el-form-item>
        <el-form-item label="数据库IP" prop="dbIp">
          <el-select
            v-model="sourceForm.dbIp"
            @change="dbIpWatch = sourceForm.dbIp"
            placeholder="请选择任务分组"
            :disabled="constData"
          >
            <el-option
              v-for="item in dbIpOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据库Port" prop="dbPort">
          <el-select
            v-model="sourceForm.dbPort"
            placeholder="请选择数据库Port"
            :disabled="constData"
          >
            <el-option
              v-for="item in dbPortOption"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="sourceForm.username"
            placeholder="请输入用户名"
            :disabled="constData"
          />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="sourceForm.password"
            :disabled="constData"
            placeholder="请输入密码"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSourceForm">确 定</el-button>
        <el-button @click="cancelSource">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTenantInfo,
  getTenantInfo,
  delTenantInfo,
  addTenantInfo,
  updateTenantInfo,
} from "@/api/tenant/tenantInfo";

import { getDbIpOption, getDbPortOption } from "@/api/source/base";
import { create, selectDataSource } from "@/api/source/data";
export default {
  name: "TenantInfo",
  dicts: ["tenant_type", "tenant_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 后台租户表格数据
      tenantInfoList: [],
      // 弹出层标题
      title: "",
      //是否为不可修改数据
      constData: false,
      // 是否显示弹出层
      open: false,
      //数据库配置弹框
      sourceOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantName: null,
        tenantType: null,
      },
      //数据库IP监听器
      dbIpWatch: null,
      //数据库IpOption
      dbIpOption: [],
      dbPortOption: [],
      // 表单参数
      form: {},
      //数据库表单参数
      sourceForm: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  watch: {
    dbIpWatch: function (newVal, oldVal) {
      if (newVal != null) {
        getDbPortOption(newVal).then((response) => {
          this.dbPortOption = response.data;
        });
      }
    },
  },
  methods: {
    // 数据库配置
    configDataSource(row) {
      this.title = "数据库资源选择配置";
      this.restSource();
      selectDataSource(row.id).then((response) => {
        if (response.data == undefined) {
          this.constData = false;
          this.sourceForm.tenantId = row.id;
          this.sourceForm.tenantType = row.tenantType;
          //并且查询数据库Ip信息
          getDbIpOption().then((response) => {
            this.dbIpOption = response.data;
          });
        } else {
          this.sourceForm = response.data;
          this.constData = true;
        }
        this.sourceOpen = true;
      });
    },
    //选择数据信息
    /** 查询后台租户列表 */
    getList() {
      this.loading = true;
      listTenantInfo(this.queryParams).then((response) => {
        this.tenantInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //取消数据库按钮
    cancelSource() {
      this.sourceOpen = false;
      this.restSource();
    },
    //数据库配置重置
    restSource() {
      this.sourceForm = {
        tenantId: null,
        dbName: null,
        dbIp: null,
        dbPort: null,
        username: null,
        password: null,
      };
      this.resetForm("restSource");
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tenantName: null,
        tenantAvatar: null,
        tenantTel: null,
        tenantLeader: null,
        tenantAppid: null,
        tenantAppsecret: null,
        tenantType: null,
        tenantAddress: null,
        tenantLongitude: null,
        tenantLatitude: null,
        tenantTime: 1,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加后台租户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTenantInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改后台租户";
      });
    },
    //提交数据库资源按钮
    submitSourceForm() {
      create(this.sourceForm).then((response) => {
        this.sourceOpen = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTenantInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTenantInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除后台租户编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTenantInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "tenant/tenantInfo/export",
        {
          ...this.queryParams,
        },
        `tenantInfo_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
